package view;

import controller.Commandable;
import java.awt.BorderLayout;
import java.awt.Container;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import visualization.PairEntry;
import visualization.Visualization;
import visualization.VisualizationFactory;


/**
 * View class that holds a Visualization and gives commands to a Commandable command
 * processing object.
 * 
 * @author Niel Lebeck and Ben Schwab
 * 
 */
@SuppressWarnings("serial")
public class WebHistoryView extends JFrame {
    private static final int WIDTH = 600;
    private static final int HEIGHT = 700;
    private static final int TEXT_AREA_COLUMNS = 20;
    private static final int TEXT_AREA_ROWS = 7;
    private static final String COMMAND_PROMPTS =
            "DATA LOADING:\n\nload data 'filename'\n\nVISUALIZATION CREATION:" +
            "\n\ncreate 'visualization type' 'data type'\n\nOptions for visualization" +
            " type:\npieChart \ncircleGraph \n\nOptions for data type:\ntimeSpent" +
            " \nnumVisits \n\nVISUALIZATION COMMANDS:\n\nrandomizeColors\naddTitle 'title' ";

    private Visualization myVisualization;
    private JScrollPane myScrollPane;
    private Console myConsole;
    private Commandable<WebHistoryView, ?> myCommandTarget;
    private JMenuBar myMenuBar;

    /**
     * Constructor.
     * @param commandTarget a Commandable object to send commands to
     */
    public WebHistoryView (Commandable<WebHistoryView, ?> commandTarget) {
        myCommandTarget = commandTarget;
        myCommandTarget.setView(this);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        // add visualization to frame
        myScrollPane = new JScrollPane(new JLabel(new ImageIcon("splash_gediva.jpg")));
        add(myScrollPane, BorderLayout.CENTER);
        initializeConsole();
        // put in some dummy text
        createMenuBar();
        setSize(WIDTH, HEIGHT);
        setTitle("WebHistoryVisualized");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void createMenuBar () {
        myMenuBar = new JMenuBar();
        JMenu help = new JMenu("Help");
        JTextArea consoleCommands = new JTextArea(TEXT_AREA_ROWS, TEXT_AREA_COLUMNS);
        consoleCommands.setText(COMMAND_PROMPTS);
        help.add(consoleCommands);
        myMenuBar.add(help);
        setJMenuBar(myMenuBar);
    }

    private void initializeConsole () {
        myConsole = new Console(myCommandTarget);
        add(myConsole, BorderLayout.PAGE_END);

    }


    private void resetView () {
        myScrollPane.setViewportView(myVisualization);
        myScrollPane.revalidate();
        new Thread(myVisualization).start();
        repaint();
    }

    /**
     * Create a Visualization that takes pairs of categorical and quantitative data as input.
     * @param viewType String indicating type of Visualization
     * @param pairedData list of paired data
     */
    public void createPairedVisualization (String viewType,
            List<PairEntry<String, Double>> pairedData) {
        try {
            myVisualization = VisualizationFactory.createVisualization(viewType, pairedData);
            resetView();
        }
        catch (WebHistoryException e) {
            Console.appendConsoleText(e.getMessage());
        }
    }

    /**
     * Randomize the visualization's colors.
     */
    public void randomizeColors () {
        myVisualization = myVisualization.randomizeColors();
        resetView();
    }
    
    public void setVisualizationTitle(String title){
        myVisualization.setTitle(title);
    }
}
